package com.biz.primus.ms.promotion.trans;

import com.biz.primus.base.vo.promotion.PromotionProductVo;
import com.biz.primus.model.promotion.vo.UserFirstOrderCutPromotionVo;
import com.biz.primus.ms.promotion.dao.po.UserFirstOrderCutPromotion;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;

/**
 * 转换器 UserFirstOrderPromotion -> UserFirstOrderPromotionVo
 *
 * @author david-liu
 */
public class UserFirstOrderPromotionPo2VoMapper implements Function<UserFirstOrderCutPromotion, UserFirstOrderCutPromotionVo> {
    @Override
    public UserFirstOrderCutPromotionVo apply(UserFirstOrderCutPromotion userFirstOrderCutPromotion) {
        UserFirstOrderCutPromotionVo vo = new UserFirstOrderCutPromotionVo();
        vo.setId(userFirstOrderCutPromotion.getId());
        vo.setName(userFirstOrderCutPromotion.getName());
        vo.setDescription(userFirstOrderCutPromotion.getDescription());
        vo.setChannels(userFirstOrderCutPromotion.getChannels());
        vo.setScope(userFirstOrderCutPromotion.getScope());
        vo.setScopeIds(StringUtils.join(userFirstOrderCutPromotion.getScopeIds(), ","));
        vo.setEffectMemberLevels(userFirstOrderCutPromotion.getEffectMemberLevelStringList());
        if (Objects.nonNull(userFirstOrderCutPromotion.getStartTime())) {
            vo.setStartTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userFirstOrderCutPromotion.getStartTime()));
        }
        if (Objects.nonNull(userFirstOrderCutPromotion.getEndTime())) {
            vo.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userFirstOrderCutPromotion.getEndTime()));
        }
        vo.setCommonStatus(userFirstOrderCutPromotion.getCommonStatus());
        vo.setCreateTimestamp(userFirstOrderCutPromotion.getCreateTimestamp());
        vo.setCutAmount(userFirstOrderCutPromotion.getCutAmount());
        vo.setProductAmountLimit(userFirstOrderCutPromotion.getProductAmountLimit());
        return vo;
    }

    public UserFirstOrderCutPromotionVo map(UserFirstOrderCutPromotion userFirstOrderCutPromotion, List<PromotionProductVo> excludeProducts) {
        UserFirstOrderCutPromotionVo vo = this.apply(userFirstOrderCutPromotion);
        vo.setExcludeProducts(excludeProducts);
        return vo;
    }

    public List<UserFirstOrderCutPromotionVo> mapWithList(List<UserFirstOrderCutPromotion> userFirstOrderCutPromotions, Map<Long, List<PromotionProductVo>> excludeProducts) {
        return userFirstOrderCutPromotions.stream().map(userFirstOrderCutPromotion -> this.map(userFirstOrderCutPromotion, excludeProducts.get(userFirstOrderCutPromotion.getId()))).collect(Collectors.toList());
    }
}
